import quotationRecordApi from '@/api/quotation/QuotationRecordApi.js'
import productApi from '@/api/project/ProductApi.js'
import utils from "@/common/utils";

export default {
    name: "quotationRecordDetail",
    data () {
        return {
            searchFormData:{
                name:'',
                modelName:''
            },
             // 参数
             params: {
                name: '',
                typeId: '',
                modelId: '',
                price: '',
                remark: '',
            },
            formDialogTitle:'',
            formDialogVisible:false,
            dialogTableData:[],
            allHistoryTableData:[],
            historyDialogVisible:false,
            tableData:[],
            pageSize:12,
            pageOn:1,
            isInfiniteScrollDisabled: false,
            query:{}
        }
    },
    methods: {
        //重置事件
        reset(){
            this.searchFormData = {
                name:'',
                modelName:''
            }
            this.pageOn = 1
            this.pageSize = 12
            this.tableData = []
            this.page()
        },
        searchData(){
            this.pageOn = 1
            this.pageSize = 12
            this.tableData = []
            this.page()
        },
        page(){
            this.isInfiniteScrollDisabled = true//避免多次请求，设置成true，请求成功后，在设置成false（58行）
            this.$rq.query(productApi.url, {
                name:this.searchFormData.name,
                modelName:this.searchFormData.modelName,
                pageSize: this.pageSize,
                pageOn: this.pageOn
            }).then(res => {
                if (res.data && Array.isArray(res.data.rows) && res.data.rows.length) {
                    this.isInfiniteScrollDisabled = false
                    if (res.data.rows.length > 0) {
                        res.data.rows.forEach(e => {
                            this.tableData.push(e)
                        })
                        this.tableData = [...new Set(this.tableData.map(JSON.stringify))].map(JSON.parse)
                        if(Math.ceil(res.data.total / this.pageSize)>this.pageOn){
                            this.isInfiniteScrollDisabled = false
                            this.pageOn +=1
                        }else{
                            this.isInfiniteScrollDisabled = true
                        }
                    }
                } else {
                    this.tableData = []
                }
            }).finally(() => {})
        },
        //产品点击+事件
        procuctClick(val){
            this.formDialogVisible = true
            this.formDialogTitle = val.name
            this.dialogTableData = val.configurations
            for(let i in val.configurations){
                this.dialogTableData[i].unitPrice = val.configurations[i].total + val.price
            }
        },
        //弹框关闭事件
        formClose(){
            this.formDialogVisible = false
        },
        //保存事件
        formSave(){
            this.formDialogVisible = false
            this.dialogTableData.forEach(e => {
                if(e.num === undefined){
                    return
                }
                if(e.discount === undefined){
                    e.discount = 10
                }
                this.allHistoryTableData.push(e)
            })
        },
        // 移除行
        removeLine(data, index) {
            this.allHistoryTableData.splice(index, 1)
        },
        //历史事件
        historyClick(){
            this.historyDialogVisible = true
        },
        //历史弹框关闭
        historyClose(){
            this.historyDialogVisible = false
        },
        //历史弹框保存
        historySave(){
            let details = []
            let total = 0
            this.allHistoryTableData.forEach(e => {
                details.push({
                    productId:e.productId,
                    optionalConfigurationId:e.id,
                    num:e.num,
                    unitPrice:e.unitPrice,
                    discount:e.discount,
                    totalPrice:parseFloat(e.num) * parseFloat(e.unitPrice) * parseFloat(e.discount)/10,
                    remark:e.remark
                })
                total += parseFloat(e.num) * parseFloat(e.unitPrice) * parseFloat(e.discount)/10
            })
            let params = {
                name:this.query.name,
                purpose:this.query.purpose,
                reamrk:this.query.remark,
                total:total,
                details:details
            }
            this.$rq.insert(quotationRecordApi.url, {...params}).then(res => {
                if(res.result === 'success'){
                    this.$message.success('保存成功')
                    this.historyDialogVisible = false
                    this.$tab.closeCurrentTab()
                }
            })
        },
    },
    mounted () {
       this.page()
    },
    created() {
        this.query = this.$route.query
    }
}
